package com.huluobo.a2206douyindemo.setting;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.lifecycle.Observer;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import com.blankj.utilcode.util.ToastUtils;
import com.huluobo.a2206douyindemo.R;
import com.huluobo.a2206douyindemo.databinding.ActivitySettingBinding;
import com.huluobo.a2206douyindemo.utils.ImageUtils;
import com.huluobo.base.bean.CommonBeanResult;
import com.huluobo.base.mvvm.BaseMVVMActivity;
import com.wildma.pictureselector.PictureBean;
import com.wildma.pictureselector.PictureSelector;

public class SettingActivity extends BaseMVVMActivity<ActivitySettingBinding, SettingViewModel> {

    @Override
    protected void initData() {
        viewModel.updateIconSuccess.observe(this, commonBeanResult -> {
            ToastUtils.showLong(commonBeanResult.getMessage());
            ImageUtils.getInstance().loadImage(this, commonBeanResult.getData(), binding.settingUserIconIv);
        });

        viewModel.updateIconFailed.observe(this, s -> {
            Log.i("TagA", "failed:" + s);
        });
    }

    @Override
    protected void initView() {
        binding.settingUserIconTv.setOnClickListener(v -> {
            selectPic();
        });
        binding.settingUserIconIv.setOnClickListener(v -> {
            selectPic();
        });
    }

    private void selectPic() {
        PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture();
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            Bundle bundle = data.getExtras();
            PictureBean pictureBean = bundle.getParcelable(PictureSelector.PICTURE_RESULT);
            String path = pictureBean.getPath();
            viewModel.updateUserIcon(path);
        }
    }

    @Override
    protected Class<SettingViewModel> getViewModel() {
        return SettingViewModel.class;
    }

    @Override
    protected int getLayoutId() {
        return R.layout.activity_setting;
    }
}